package com.gmail.at.jra.felipe.view;

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;

/**
 * This is a simple implementation of a client for the SMTP protocol.
 * */
public class MainGUI extends JFrame {
	
	protected JTextField from = null;
	protected JTextField to = null;
	protected JScrollPane writingArea = null;
	protected JTextField subject = null;
	protected JButton send = null;
	protected JButton cancel = null;
	protected JButton clear = null;
	
	/**
	 * Constructor.
	 * Initializes all GUI items.
	 * */
	public MainGUI()
	{
		super("SMTP Mail Client");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//this.setExtendedState(MAXIMIZED_BOTH);
		this.setSize(400, 400);
		this.initComponents();
		this.setVisible(true);
	}
	
	/**
	 * Initializes GUI items.
	 * */
	public void initComponents()
	{
		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);
		this.add(this.getUpperPanel(), BorderLayout.NORTH);
		this.add(this.getLowerPanel(), BorderLayout.SOUTH);
		this.add(this.getWritingAreaJScrollPane(), BorderLayout.CENTER);
	}
	
	private JTextField getToJTextField()
	{
		if( this.to != null ) return this.to;
		
		to = new JTextField(30);
		return to;
	}
	
	private JTextField getFromJTextField()
	{
		if( this.from != null ) return this.from;
		
		from = new JTextField(30);
		return from;
	}
	
	private JScrollPane getWritingAreaJScrollPane()
	{
		if( this.writingArea != null) return this.writingArea;
		JTextArea ta = new JTextArea();
		ta.setLineWrap(true);
		ta.setWrapStyleWord(true);
		ta.setMargin(new Insets(10, 10, 10, 10));
		writingArea = new JScrollPane( ta );
		//writingArea.setWrapStyleWord(true);
		
		return writingArea;
	}
	
	private JButton getSendJButton()
	{
		if( this.send != null) return this.send;
		
		send = new JButton("Send");
		return send;
	}
	
	private JButton getCancelJButton()
	{
		if( this.cancel != null ) return this.cancel;
		
		cancel = new JButton("Cancel");
		return cancel;
	}
	
	private JButton getClearJButton()
	{
		if( this.clear != null ) return this.clear;
		
		clear = new JButton("Clear");
		return clear;
	}
	
	private JPanel getUpperPanel()
	{
		JPanel panel = new JPanel(new GridBagLayout());
		
		GridBagConstraints gbcLabelFrom = new GridBagConstraints();
		gbcLabelFrom.fill = GridBagConstraints.NONE;
		gbcLabelFrom.gridwidth = 1;
		gbcLabelFrom.gridheight = 1;
		gbcLabelFrom.gridx = 0;
		gbcLabelFrom.gridy = 0;
		gbcLabelFrom.ipadx = 5;
		gbcLabelFrom.ipady = 5;
		gbcLabelFrom.insets = new Insets(20, 2, 2, 2);
		GridBagConstraints gbcFrom = new GridBagConstraints();
		gbcFrom.fill = GridBagConstraints.BOTH;
		gbcFrom.weightx = 1;
		gbcFrom.gridwidth = 5;
		gbcFrom.gridheight = 1;
		gbcFrom.gridx = 1;
		gbcFrom.gridy = 0;
		gbcFrom.ipadx = 5;
		gbcFrom.ipady = 5;
		gbcFrom.insets = new Insets(20, 2, 2, 2);
		
		GridBagConstraints gbcLabelTo = new GridBagConstraints();
		gbcLabelTo.fill = GridBagConstraints.NONE;
		gbcLabelTo.gridx = 0;
		gbcLabelTo.gridy = 1;
		gbcLabelTo.ipadx = 5;
		gbcLabelTo.ipady = 5;
		gbcLabelTo.insets = new Insets(2, 2, 20, 2);
		GridBagConstraints gbcTo = new GridBagConstraints();
		gbcTo.fill = GridBagConstraints.BOTH;
		gbcTo.weightx = 1;
		gbcTo.gridwidth = 5;
		gbcTo.gridx = 1;
		gbcTo.gridy = 1 ;
		gbcTo.ipadx = 5;
		gbcTo.ipady = 5;
		gbcTo.insets = new Insets(2, 2, 20, 2);
		
		
		
		panel.add( new JLabel("From: "), gbcLabelFrom );
		panel.add( this.getFromJTextField(), gbcFrom );
		panel.add( new JLabel("To: "), gbcLabelTo );
		panel.add( this.getToJTextField(), gbcTo);
		
		
		
		return panel;
	}
	
	private JPanel getLowerPanel()
	{
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints gbcSend = new GridBagConstraints();
		gbcSend.gridx = 0;
		gbcSend.gridy = 0;
		gbcSend.insets = new Insets(10, 10, 10, 10);
		
		GridBagConstraints gbcCancel = new GridBagConstraints();
		gbcCancel.gridx = 1;
		gbcCancel.gridy = 0;
		gbcCancel.insets = new Insets(10, 10, 10, 10);
		
		GridBagConstraints gbcClear = new GridBagConstraints();
		gbcClear.gridx = 2;
		gbcClear.gridy = 0;
		gbcClear.insets = new Insets(10, 10, 10, 10);
		
		panel.add( this.getSendJButton(), gbcSend );
		panel.add( this.getClearJButton(), gbcClear );
		panel.add( this.getCancelJButton(), gbcCancel );
		
		return panel;
	}

}
